# -*- coding: utf-8 -*-
import mysqlDB
class O2oOrderStock:
    def __init__(self):
        """

        :rtype : object
        """
        self.odb =mysqlDB.Mydb(host='localhost', dataBase='smw01_erp0911', user='smw01_erp0911', passwd='ZMwj3BSwtVTMS32r')
    #------------------------------------------ End def __init__()

    def update(self):
        self.odb.dbLoad()
        query = self.odb.query(u"SELECT sum(og.number) onumber,og.product_id product_id FROM `order` as o LEFT JOIN order_goods as og ON o.id=og.order_id WHERE o.id>16253 AND o.shipping_status=0 GROUP BY og.product_id ORDER BY onumber DESC;")
        for row in query:
            sql="SELECT id,stock,localstock,orderstock FROM product WHERE id=%s;" % (row['product_id'])
            info=self.odb.query(sql)
            res=info[0]
            res['orderstock']=row['onumber']
            res['stock']=res['localstock']-row['onumber']
            if res['stock']<0:
                res['stock']=0
            print("%s   占用  %s  库存 %s  可用  %s" % (row['product_id'],row['onumber'],res['localstock'],res['stock']))
            instesql="UPDATE `product` SET `stock`=%s,`orderstock`='%s' WHERE ( `id` = %s );" % (res ['stock'],res['orderstock'],res['id'])
            print(instesql)
            self.odb.save(instesql)
        self.odb.dbClose()
    #------------------------------------------ End def assets()
#-------------------------------------------------- End class UserAssets

if __name__ == '__main__':
    desc=O2oOrderStock()
    desc.update()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
